home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER3 / BCONNECT.C next >
C/C++ Source or Header  |  1996-04-27  |  12KB  |  378 lines

  1.  
  2. #include <windows.h>  
  3. #include "bconnect.h"
  4. #include "sqlext.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLBrowseConnect()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106. #define BUFFER_LEN 256
  107.  
  108. BOOL   bOpt;
  109. LPTSTR lpParam;
  110. LPTSTR lpName;
  111. LPTSTR lpOptions;
  112. UCHAR  szConnStrIn [BUFFER_LEN+1];
  113.  
  114.  
  115. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  116. {
  117. static HENV hEnv  = NULL;
  118. static HDBC hDBC  = NULL;
  119. static HWND hList = NULL;
  120.  
  121.    switch( uMsg )
  122.    {
  123.       case WM_CREATE  :
  124.               // Allocate Environment and Connection.
  125.               //.....................................
  126.               SQLAllocEnv( &hEnv );
  127.               SQLAllocConnect( hEnv, &hDBC );
  128.  
  129.               hList = CreateWindow( "LISTBOX", "",    
  130.                                     LBS_NOTIFY | WS_VSCROLL | 
  131.                                     WS_BORDER  | WS_CHILD | 
  132.                                     WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  133.                                     0, 0, 
  134.                                     0, 0,  
  135.                                     hWnd,              
  136.                                     (HMENU)101,              
  137.                                     hInst,         
  138.                                     NULL               
  139.                                   );
  140.  
  141.               break;
  142.  
  143.       case WM_SIZE :
  144.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  145.                                        HIWORD( lParam ), TRUE );
  146.               break; 
  147.               
  148.       case WM_COMMAND :
  149.               switch( LOWORD( wParam ) )
  150.               {
  151.                  case IDM_TEST :
  152.                         {
  153.                            RETCODE retCode;
  154.                            UCHAR   szConnStrOut[BUFFER_LEN+1];
  155.                            SWORD   cbConnStrOut;
  156.  
  157.                            strcpy( szConnStrIn, "DSN=Test Data Source" );
  158.  
  159.                            // Connect to the data source
  160.                            //...........................
  161.                            do
  162.                            {
  163.                               retCode = SQLBrowseConnect( hDBC, 
  164.                                            szConnStrIn,   SQL_NTS,
  165.                                            szConnStrOut,  BUFFER_LEN,
  166.                                            &cbConnStrOut );
  167.                               
  168.                               // Call an external function to get
  169.                               // additional connection data.
  170.                               // ................................
  171.                               if( retCode == SQL_NEED_DATA  )
  172.                                  if ( !GetInputFromUser( hWnd, szConnStrOut ) )
  173.                                     return( 0L );
  174.                            }
  175.                            while( retCode == SQL_NEED_DATA );
  176.  
  177.                            // Show connection string / status.
  178.                            //.................................
  179.                            if( retCode == SQL_SUCCESS || 
  180.                                retCode == SQL_SUCCESS_WITH_INFO )
  181.                            {
  182.                               SendMessage( hList, LB_ADDSTRING, 
  183.                                  0, (LPARAM)"Connection established" );
  184.                            }
  185.                            else
  186.                            {
  187.                               SendMessage( hList, LB_ADDSTRING, 
  188.                                  0, (LPARAM)"Unable to establish connection" );
  189.                            }
  190.                         }
  191.                         break;
  192.  
  193.                  case IDM_ABOUT :
  194.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  195.                         break;
  196.  
  197.                  case IDM_EXIT :
  198.                         DestroyWindow( hWnd );
  199.                         break;
  200.               }
  201.               break;
  202.       
  203.       case WM_DESTROY :
  204.               // Disconnect Environment.
  205.               //........................
  206.               if ( hDBC )
  207.                  SQLDisconnect( hDBC );
  208.  
  209.               // Free Connection and Environment.
  210.               //.................................
  211.               SQLFreeConnect( hDBC );
  212.               SQLFreeEnv( hEnv );
  213.  
  214.               PostQuitMessage(0);
  215.               break;
  216.  
  217.       default :
  218.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  219.    }
  220.  
  221.    return( 0L );
  222. }
  223.  
  224.  
  225. BOOL GetInputFromUser( HWND hWnd, LPTSTR lpszConnStrOut )
  226. {
  227.    LPTSTR lpCur   = lpszConnStrOut;
  228.  
  229.    while ( *lpCur )
  230.    {
  231.       // Check to see if it is optional.
  232.       //................................
  233.       if ( *lpCur == '*' )
  234.       {
  235.          bOpt = TRUE;
  236.          lpCur++;
  237.       }
  238.       else 
  239.          bOpt = FALSE;
  240.  
  241.       // Get the Parameter Name.
  242.       //........................
  243.       lpParam = lpCur;
  244.       while( *(++lpCur) != ':' );
  245.  
  246.       *lpCur++ = '\0';
  247.  
  248.       // Get the Local Name.
  249.       //....................
  250.       lpName = lpCur;
  251.       while( *(++lpCur) != '=' );
  252.  
  253.       *lpCur++ = '\0';
  254.  
  255.       // Get the options.
  256.       //.................
  257.       lpOptions = lpCur;
  258.       while( *lpCur && *lpCur != ';' )
  259.          lpCur++;
  260.  
  261.       if ( *lpCur )
  262.          *lpCur++ = '\0';
  263.  
  264.       if ( DialogBox( hInst, "InputDialog", hWnd, GetInputProc ) == IDCANCEL )
  265.          return( FALSE );  
  266.    }
  267.  
  268.    return( TRUE );
  269. }
  270.  
  271.  
  272. LRESULT CALLBACK GetInputProc( HWND hDlg,           
  273.                                UINT message,        
  274.                                WPARAM wParam,       
  275.                                LPARAM lParam)
  276. {
  277. static BOOL bList;
  278.  
  279.    switch (message) 
  280.    {
  281.        case WM_INITDIALOG: 
  282.                bList = ( *lpOptions == '{' );
  283.                
  284.                SetDlgItemText( hDlg, IDC_LOCALNAME, lpName );
  285.  
  286.                ShowWindow( GetDlgItem( hDlg, IDC_EDIT ),
  287.                            bList ? SW_HIDE : SW_SHOW );
  288.  
  289.                ShowWindow( GetDlgItem( hDlg, IDC_LIST ),
  290.                            bList ? SW_SHOW : SW_HIDE );
  291.  
  292.                ShowWindow( GetDlgItem( hDlg, IDC_OPTIONAL ), 
  293.                            bOpt ? SW_SHOW : SW_HIDE );
  294.  
  295.                // If this is a list, parse the different options
  296.                // and display them in the list box.
  297.                //...............................................
  298.                if ( bList )
  299.                {
  300.                   LPTSTR lpBeg = lpOptions+1;
  301.                   LPTSTR lpCur = lpOptions+1;
  302.  
  303.                   while( *lpCur != '}')
  304.                   {
  305.                      if ( *lpCur == ',' )
  306.                      {
  307.                         *lpCur = '\0';
  308.                         SendDlgItemMessage( hDlg, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)lpBeg );
  309.                         lpCur++;
  310.                         lpBeg = lpCur;
  311.                      }
  312.                      else
  313.                         lpCur++;
  314.                   }
  315.                   *lpCur = '\0';
  316.                   SendDlgItemMessage( hDlg, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)lpBeg );
  317.                   SendDlgItemMessage( hDlg, IDC_LIST, LB_SETCURSEL, 0, 0 );
  318.  
  319.                }
  320.                return (TRUE);
  321.  
  322.        case WM_COMMAND:                              
  323.                switch( LOWORD( wParam ) )
  324.                {
  325.                   case IDOK :
  326.                         {
  327.                            TCHAR szValue[64];
  328.  
  329.                            if ( bList )
  330.                               SendDlgItemMessage( hDlg, IDC_LIST, LB_GETTEXT,
  331.                                                   SendDlgItemMessage( hDlg, IDC_LIST, LB_GETCURSEL, 0, 0 ), 
  332.                                                   (LPARAM)szValue );
  333.                            else
  334.                               GetDlgItemText( hDlg, IDC_EDIT, szValue, sizeof( szValue )-1 );
  335.  
  336.                            strcat( szConnStrIn, ";" );
  337.                            strcat( szConnStrIn, lpParam );
  338.                            strcat( szConnStrIn, "=" );
  339.                            strcat( szConnStrIn, szValue );
  340.  
  341.                            EndDialog( hDlg, IDOK );
  342.                         }
  343.                         return( TRUE );
  344.  
  345.                   case IDCANCEL :
  346.                         EndDialog( hDlg, IDCANCEL );
  347.                         return( TRUE );
  348.                }
  349.                break;
  350.    }
  351.  
  352.    return (FALSE); 
  353. }
  354.  
  355.  
  356. LRESULT CALLBACK About( HWND hDlg,           
  357.                         UINT message,        
  358.                         WPARAM wParam,       
  359.                         LPARAM lParam)
  360. {
  361.    switch (message) 
  362.    {
  363.        case WM_INITDIALOG: 
  364.                return (TRUE);
  365.  
  366.        case WM_COMMAND:                              
  367.                if (   LOWORD(wParam) == IDOK         
  368.                    || LOWORD(wParam) == IDCANCEL)    
  369.                {
  370.                        EndDialog(hDlg, TRUE);        
  371.                        return (TRUE);
  372.                }
  373.                break;
  374.    }
  375.  
  376.    return (FALSE); 
  377. }
  378.